package com.wjk.kylin.core.common.data;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjk.kylin.core.utils.HttpContextUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wangjkui
 * @description 请求数据
 * @date 2020/5/22
 */
public class RequestData<T> extends Page<T> {

    /**
     * @param defaultPageSize 默认分页大小，如果传递 -1 则为不分页，返回所有数据
     */
    public RequestData(int defaultPageSize) {

        this();

        if (defaultPageSize == -1) {
            super.setSize(defaultPageSize);
        }

    }

    /**
     * 分页参数
     *
     * @return
     */
    public RequestData() {
        int pageSize = 20;//每页记录数
        int pageNum = 1; //页数
        HttpServletRequest httpServletRequest = HttpContextUtils.getHttpServletRequest();
        String pageSizeStr = httpServletRequest.getParameter("pageSize");
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        super.setSize(pageSize);

        String pageNumSt = httpServletRequest.getParameter("pageNum");
        if (StringUtils.isNotBlank(pageNumSt)) {
            pageNum = Integer.parseInt(pageNumSt);
        }
        super.setCurrent(pageNum);

        //========================================排序=============================
//        String orderBy = httpServletRequest.getParameter("orderBy");
//
//        if(StringUtils.isNotBlank(orderBy)){
//            String asc = httpServletRequest.getParameter("asc");
//            if(StringUtils.equals("true",asc)){
//                super.addOrder(OrderItem.ascs(StrUtil.toSymbolCase(orderBy,'_')));
//            }else{
//                super.addOrder(OrderItem.desc(StrUtil.toSymbolCase(orderBy,'_')));
//            }
//        }else{
//            super.addOrder(OrderItem.desc("update_date"));
//        }
    }

}
